//
//  DestinationViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/13.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "DestinationViewController.h"
#import "DestinationCell.h"
#import <AFNetworking/AFNetworking.h>
#import "City.h"
#import "CityCollection.h"
#import "CityCollectionViewCell.h"
#import "CityDetailViewController.h"
#import "FanProgress3D/FanProgress3D.h"

#define kCityUrl @"http://apiphp.yaochufa.com/portal/dest/provinceList?version=4.4.0&imei=00000000-586f-a266-a828-d02c62cce401&system=ios&deviceToken=00000000-586f-a266-a828-d02c62cce401&regId=040f3d14659&channel=yingyongbao%20"//城市请求
#define kCityCollectionUrl @"http://apiphp.yaochufa.com/portal/dest/scenicCity?areaCode=%@&system=ios&listType=province&channel=AppStore&version=4.4.0&imei=853B8C69-003C-488D-BFF8-1A21640D8184%%20"

#define kCityDetailUrl @"http://apiphp.yaochufa.com/portal/scenic/ScenicDetail430?version=4.4.0&id=%@&system=ios&channel=AppStore&imei=853B8C69-003C-488D-BFF8-1A21640D8184"

@interface DestinationViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (strong, nonatomic) IBOutlet UITableView *cityTableView;
@property (strong, nonatomic) IBOutlet UICollectionView *cityCollectionView;
@property (strong, nonatomic) NSMutableArray *cityArray;
@property (strong, nonatomic) NSMutableArray *cityCollectionArray;
@property (strong, nonatomic) IBOutlet UILabel *hotCityLabel;//热门城市

@end
static NSString *indexPathRow = @"0";

@implementation DestinationViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [FanProgress3D showInView:self.view status:@"全景载入中...."];
    [self loadTableView];
    [self loadCollectionView];
    [self loadCityData];
    [self loadCollectionData];
    
    
    NSInteger selectedIndex = 0;
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
    [self.cityTableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    

}
- (void)loadCityData{
    
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    self.cityArray = [NSMutableArray array];
    //2.发送请求
    [mgr GET:kCityUrl parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *array = responseObject[@"content"];
        for (NSDictionary *dict in array) {
            City *cityModel = [[City alloc]init];
            [cityModel setValuesForKeysWithDictionary:dict];
            [self.cityArray addObject:cityModel];
        }
        [self.cityTableView reloadData];
        [FanProgress3D dismiss];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
    }];
    
}
- (void)loadCollectionData{
    
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    self.cityCollectionArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:kCityCollectionUrl,indexPathRow];
    //2.发送请求
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *dict = responseObject[@"content"];
        NSArray *array = dict[@"hotScenic"];
        for (NSDictionary *dic in array) {
            CityCollection *coModel = [[CityCollection alloc]init];
            [coModel setValuesForKeysWithDictionary:dic];
            [self.cityCollectionArray addObject:coModel];
        }
        [self.cityCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
    }];

}
#pragma mark -tableView
- (void)loadTableView{
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    [self.cityTableView registerNib:[UINib nibWithNibName:@"DestinationCell" bundle:nil] forCellReuseIdentifier:@"DestinationCell"];  
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cityArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"DestinationCell";
    DestinationCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.row == 0) {
     
        cell.selected = YES;
        
    }
    City *cityModel = self.cityArray[indexPath.row];
    
    
    cell.cityName.text = cityModel.name;
    cell.cityName.highlightedTextColor = [UIColor colorWithRed:3/255.0 green:189/255.0 blue:249/255.0 alpha:1.0];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    City *city = self.cityArray[indexPath.row];
    indexPathRow = city.areaCode;
    self.hotCityLabel.text = [NSString stringWithFormat:@"%@热门景区",city.name];
    
    [self loadCollectionData];
    
    
}


#pragma mark--collectionView
- (void)loadCollectionView{
    self.cityCollectionView.delegate = self;
    self.cityCollectionView.dataSource = self;
    [self.cityCollectionView registerNib:[UINib nibWithNibName:@"CityCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CityCollectionViewCell"];
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.cityCollectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"CityCollectionViewCell";
    CityCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    CityCollection *coModel = self.cityCollectionArray[indexPath.row];
    [cell bindModel:coModel];
    
    return cell;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake((self.view.frame.size.width - 140)/2, 100);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 10, 20, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CityDetailViewController *cityDetailVC = [[CityDetailViewController alloc]init];
    CityCollection *model = self.cityCollectionArray[indexPath.row];
    
    cityDetailVC.urlStr = [NSString stringWithFormat:kCityDetailUrl,model.scenicId];
    cityDetailVC.navigationItem.title = model.scenicName;
    
    [self.navigationController pushViewController:cityDetailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
